<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 2018/2/5
 * Time: 上午10:34
 */

namespace app\controller;


use app\model\Admin;
use app\model\AdminLog;
use app\model\AgentList;
use app\model\CompanyList;
use think\Request;

/**
 * @property Admin Admin
 * @property AdminLog AdminLog
 * @property AgentList AgentList
 * @property CompanyList CompanyList
 */
class Adminlist extends CommonController
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Admin = new Admin();
        $this->AdminLog = new AdminLog();
        $this->AgentList = new AgentList();
        $this->CompanyList = new CompanyList();
    }
    function index()
    {
        $role_list = [];
        foreach ($this->role_list as $val)
        {
            $role_list[] = ["id"=>$val["id"],"role_name"=>$val["role_name"]];
        }
        $agent_list = $this->AgentList->where(["status"=>0])->select();
        $company_list = $this->CompanyList->where(["status"=>0])->select();
        $this->assign("role_list",$role_list);
        $this->assign("agent_list",$agent_list);
        $this->assign("company_list",$company_list);
        $this->assign("page_title", "后台用户列表");
        return $this->fetch();
    }

    function ajax_admin_list()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");
            $mobile = input("mobile","","trim");
            $user_name = input("user_name","","trim");

            $where = [];
            if(!empty($mobile))
                $where["a.mobile"] = $mobile;
            if(!empty($user_name))
                $where["a.user_name"] = $user_name;

            $count = $this->Admin->alias("a")
                ->where($where)
                ->count();
            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $order_log = $this->Admin->alias("a")
                ->join("f_agent_list b","b.id=a.agent_id","left")
                ->join("f_company_list c","c.id=a.company_id","left")
                ->where($where)
                ->order("a.id desc")
                ->field("a.*,b.agent_name,c.name as company_name")
                ->limit(($page-1)*$rows,$rows)
                ->select();


            $this->resultdata["data"] = $order_log;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_admin_info()
    {
        try
        {
            $id = input("id",0,"intval");
            if(empty($id))
                $this->E("参数不能为空");
            $info = $this->Admin->where(["id"=>$id])->find();
            $info["role_list"] =empty($info["role_list"])?[]:explode(",",$info["role_list"]);
            unset($info["password"]);
            $this->resultdata["data"] = $info;
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }
        echo json_encode($this->resultdata);
    }
    function ajax_admin_info_save()
    {
        try
        {

            $id = input("id",0,"intval");
            $agent_id = input("agent_id",0,"intval");
            $company_id = input("company_id",0,"intval");
            $user_name = input("user_name","","trim");

            $password = input("password","","trim");
            $mobile = input("mobile","","trim");
            $real_name = input("real_name","","trim");
//            $role_list = input("role_list","","trim");
            $role_list =  input('post.role_list/a');

            $status = input("status",0,"intval");
            if(empty($role_list)||empty($user_name)||empty($real_name)||empty($mobile) || !in_array($status,["y","n"]))
                $this->E("参数不能为空1");
            if(empty($id) && empty($password))
                $this->E("参数不能为空2");
            $data = [
                "role_list"=>implode(",",$role_list),
                "user_name"=>$user_name,
                "real_name"=>$real_name,
                "mobile"=>$mobile,
                "agent_id"=>$agent_id,
                "company_id"=>$company_id,
                "status"=>$status,
                "ctime"=>NOW_TIME,
                "mtime" => NOW_TIME,
                "ip"=>"127.0.0.1"
            ];

            if(empty($id))
            {
                $user_info = $this->Admin->where(["user_name"=>$user_name])->find();
                if(!empty($user_info))
                    $this->E("登录账号己存在");
                $data["password"] = double_md5($password);
                $data["ctime"] = get_date_time();
                $query = $this->Admin->create($data);
            }else{
                unset($data["user_name"]);
                if(!empty($password))
                    $data["password"] = double_md5($password);
                $query = $this->Admin->where(['id'=>$id])->update($data);
            }
            if(!$query)
                $this->E("失败");

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function sys_log()
    {
        $this->assign("page_title", "系统操作日志");
        return $this->fetch();
    }
    function ajax_sys_log()
    {
        try
        {
            $page = input("page",1,"intval");
            $rows = input("rows",30,"intval");

            $date_1 = input("date_1",get_date_time("Y-m-d"),"trim");
            $date_2 = input("date_2",get_date_time("Y-m-d"),"trim");

            if($date_1 > $date_2)
                $this->E("选择日期出错");
            $where = [];
            $where["al.ctime"] = ["between",[$date_1." 00:00:00",$date_2." 23:59:59"]];
            $where["al.module"] = ["NEQ","adminlist.ajax_set_password"];
            $count = $this->AdminLog->alias("al")
                ->join("f_admin su","su.id=al.admin_id","left")
                ->where($where)->count();

            $page_total = ceil($count/$rows);
            $page = $page>$page_total?$page_total:$page;
            $page = $page < 1? 1:$page;
            $log_list = $this->AdminLog->alias("al")
                ->join("f_admin su","su.id=al.admin_id","left")
                ->where($where)
                ->order("al.ctime desc")
                ->field("al.*,su.real_name")
                ->limit(($page-1)*$rows,$rows)
                ->select();
            $module_list = [];
            foreach ($this->menu_list as $key=>$val)
            {
                $link = $val["link"];
                $module = explode("/",$link);
                $module[0] = !empty($module[0])?$module[0]:"";
                $module[1] = !empty($module[1])?strtolower($module[1]):"index";
                $module[2] = !empty($module[2])?strtolower($module[2]):"index";
                $module_list[$module[1].".".$module[2]]=$val["name"];
            }
            foreach ($log_list as $key=>$val)
            {
                $log_list[$key]["module_name"] = !empty($module_list[$val["module"]])?$module_list[$val["module"]]:$val["module"];
            }


            $this->resultdata["data"] = $log_list;
            $this->resultdata["error"] = 0;
            $this->resultdata["total"] = $count;
            $this->resultdata["page"] = $page;
            $this->resultdata["rows"] = $rows;
        }catch (\Exception $e)
        {

            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
    function ajax_set_password()
    {
        try
        {

            $password = input("password","","trim");
            $old_password = input("old_password","","trim");

            if(empty($password)||empty($old_password))
                $this->E("参数不能为空1");

            $user_info = $this->Admin->where(["id"=>$this->admin_id])->find();
            if(empty($user_info))
                $this->E("登录账号不存在");
            $password =double_md5($password);
            $old_password =double_md5($old_password);
            if($old_password != $user_info["password"])
                $this->E("原始密码错误");
            $data = [];
            $data["password"] = $password;
            $data["mtime"] = get_date_time();
            $query = $this->Admin->where(["id"=>$this->admin_id])->update($data);
            if(!$query)
                $this->E("失败");

            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
}